<?php

include('header.php');
include('class/YouTube.php');
$smarty->assign('media', siteinfo('media'));

$haveCategory = false;
$kategorija = '';
$kategorijaSidebarUrl = '';
//Last video to show on body
if (isset($_GET['kategorija']) && !empty($_GET['kategorija'])) {
    $kategorija = mres($_GET['kategorija']);
    if (isset($_GET['videoid']) && !empty($_GET['videoid'])){
        $mainVideo = mres($_GET['videoid']);
    } else {
        $mainVideo = getLastVideoByCategory($kategorija);
    }
    $haveCategory = true;
    $kategorijaSidebarUrl = '&kategorija=' .$kategorija;
} elseif (isset($_GET['videoid']) && !empty($_GET['videoid'])) {
    $mainVideo = mres($_GET['videoid']);
} else {
    $mainVideo = getLastVideo();
}

$notshown = $mainVideo;

$totalCommentedVideo = getNumberOfVideoComments($mainVideo);
$VideoDescription = getMainVideoDescription($mainVideo);
$VideoTags = getMainVideoTags($mainVideo);
$videoComments = getVideoComments($mainVideo);

//sidebar videos
$paginatorSidebarVideos = new Paginator;
$paginatorSidebarVideos->mid_range = 5;

if ($haveCategory) {
    $paginatorSidebarVideos->items_total = getTotalSidebarVideosByCategory($notshown, $kategorija);
    $paginatorSidebarVideos->paginate();
    $sidebarVideos = getSidebarVideosByCategory($notshown, $paginatorSidebarVideos->limit, $kategorija);
} else {
    $paginatorSidebarVideos->items_total = getTotalSidebarVideos($notshown);
    $paginatorSidebarVideos->paginate();
    $sidebarVideos = getSidebarVideos($notshown, $paginatorSidebarVideos->limit);
}


$sidebarVideosObj = array();
foreach ($sidebarVideos as $sidebarVideo) {
    $sidebarVideosObj[] = getVideo($sidebarVideo['url']);
}

//$video = getVideo('roA1T5ZVFW4');
$smarty->assign('kategorijaSidebarUrl',$kategorijaSidebarUrl);
$smarty->assign('kategorije', getVideoCategories());
$smarty->assign('paginatorSidebarVideos', $paginatorSidebarVideos->display_pages());
$smarty->assign('mainVideo', $mainVideo);
$smarty->assign('sideVideos', $sidebarVideosObj);
$smarty->assign('VideoDescription', $VideoDescription);
$smarty->assign('VideoTags', $VideoTags);
$smarty->assign('videoComments', $videoComments);
$smarty->assign('totalCommentedVideo', $totalCommentedVideo);


$smarty->display('video.tpl');
include('footer.php');
?>
